package cc.mrbird.febs.buss.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.buss.entity.AppointmentInfo;
import cc.mrbird.febs.buss.service.IAppointmentInfoService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.Enumeration;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * 预约信息 Controller
 *
 * @author jeson
 * @date 2021-04-26 11:01:03
 */
@Slf4j
@Validated
@Controller
public class AppointmentInfoController extends BaseController {

    @Autowired
    private IAppointmentInfoService appointmentInfoService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "appointmentInfo")
    public String appointmentInfoIndex(){
        return FebsUtil.view("appointmentInfo/appointmentInfo");
    }

    @GetMapping("appointmentInfo")
    @ResponseBody
    @RequiresPermissions("appointmentInfo:list")
    public FebsResponse getAllAppointmentInfos(AppointmentInfo appointmentInfo) {
        return new FebsResponse().success().data(appointmentInfoService.findAppointmentInfos(appointmentInfo));
    }

    @GetMapping("appointmentInfo/list")
    @ResponseBody
    public FebsResponse appointmentInfoList(QueryRequest request, HttpServletRequest req, AppointmentInfo appointmentInfo) {
        Enumeration enu=req.getParameterNames();
        HashMap<String,String> map=new HashMap<String,String>();
        while(enu.hasMoreElements()){
            String paraName=(String)enu.nextElement();
            map.put(paraName,req.getParameter(paraName));
        }
        Map<String, Object> dataTable = getDataTable(this.appointmentInfoService.findAppointmentInfoDetail(request, map));
        return new FebsResponse().success().data(dataTable);
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "appointmentInfo/add")
    public String goAppointmentInfoAdd() {
        return FebsUtil.view("appointmentInfo/appointmentInfoAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "appointmentInfo/update/{id}")
    public String goAppointmentInfoUpdate(@PathVariable String id, Model model) {
        AppointmentInfo obj= appointmentInfoService.getById(id);
        model.addAttribute("obj",obj);
        return FebsUtil.view("appointmentInfo/appointmentInfoUpdate");
    }

    @ControllerEndpoint(operation = "新增AppointmentInfo", exceptionMessage = "新增AppointmentInfo失败")
    @PostMapping("appointmentInfo")
    @ResponseBody
    public FebsResponse addAppointmentInfo(@Valid AppointmentInfo appointmentInfo) {
        this.appointmentInfoService.createAppointmentInfo(appointmentInfo);
        return new FebsResponse().success();
    }

    @GetMapping("appointmentInfo/delete/{objIds}")
    @ControllerEndpoint(operation = "删除", exceptionMessage = "删除失败")
    @ResponseBody
    public FebsResponse deleteAppointmentInfo( @PathVariable String objIds) {
        String[] ids = objIds.split(StringPool.COMMA);
        this.appointmentInfoService.deleteAppointmentInfo(ids);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改AppointmentInfo", exceptionMessage = "修改AppointmentInfo失败")
    @PostMapping("appointmentInfo/update")
    @ResponseBody
    public FebsResponse updateAppointmentInfo(AppointmentInfo appointmentInfo) {
        this.appointmentInfoService.updateAppointmentInfo(appointmentInfo);
        return new FebsResponse().success();
    }
}
